package com.aiyige.page.my.order.dialog;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.R;
import com.aiyige.page.my.order.model.SubjectSelection;
import com.aiyige.page.my.order.model.TimeSelectionEntity;
import com.aiyige.utils.widget.BaseBottomDialogFragment;
import com.aiyige.utils.widget.autoLayoutManager.AutoLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zhuyakun on 2017/11/13.
 */

public class OrderClassifyBottomDialog extends BaseBottomDialogFragment {

    @BindView(R.id.rv_goods_classify)
    RecyclerView recyclerViewClassify;
    @BindView(R.id.rv_order_time)
    RecyclerView recyclerViewOrderTime;

    Unbinder unbinder;

    ClassifyAdapter orderClassifyAdapter;
    OrderTimeAdapter orderTimeAdapter;
    List<SubjectSelection> classifyList;
    List<TimeSelectionEntity> timeList;

    public static OrderClassifyBottomDialog newInstance() {
        return new OrderClassifyBottomDialog();
    }

    Listener listener;

    public void setLisenter(Listener callback) {
        this.listener = callback;
    }

    public void setData(List<SubjectSelection> classifyList,List<TimeSelectionEntity> timeList) {
        this.classifyList = classifyList;
        this.timeList = timeList;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_order_classify, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        orderClassifyAdapter = new ClassifyAdapter();
        orderTimeAdapter = new OrderTimeAdapter();
        recyclerViewClassify.setAdapter(orderClassifyAdapter);
        recyclerViewOrderTime.setAdapter(orderTimeAdapter);
        AutoLayoutManager layout = new AutoLayoutManager();
        //必须，防止recyclerview高度为wrap时测量item高度0
        layout.setAutoMeasureEnabled(true);
        recyclerViewClassify.setLayoutManager(layout);
        AutoLayoutManager layout1 = new AutoLayoutManager();
        //必须，防止recyclerview高度为wrap时测量item高度0
        layout1.setAutoMeasureEnabled(true);
        recyclerViewOrderTime.setLayoutManager(layout1);
        orderTimeAdapter.addData(timeList);
        orderClassifyAdapter.addData(classifyList);
        orderTimeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                orderTimeAdapter.reset();
                timeList.get(position).setSelected(!timeList.get(position).isSelected());
                orderTimeAdapter.notifyDataSetChanged();
            }
        });
        orderClassifyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                classifyList.get(position).setSelected(!classifyList.get(position).isSelected());
                orderClassifyAdapter.notifyDataSetChanged();
            }
        });
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void cleanSelected() {
        orderClassifyAdapter.reset();
        orderTimeAdapter.reset();
    }

    private void doFinish() {
        if (listener != null) {
            listener.onFinish();
        }
        dismiss();
    }

    @OnClick({R.id.btn_reset, R.id.btn_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_reset:
                cleanSelected();
                break;
            case R.id.btn_finish:
                doFinish();
                break;
        }
    }

    public interface Listener {
        void onFinish();
    }
}
